﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WL.Model;
using WL.Dal;

namespace WL.Bll
{
    class TableService
    {
        /// <summary>
        /// 删除表格
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool delTable(string id) {
            Table t = TableDal.getTableById(Convert.ToInt32(id));

            int ret =(Dal.TableDal.delTable(id));

            if (ret > 0)
            {
                //删除字段数据
                Dal.TableDal.delFields(id);
                //drop表格
                Dal.TableDal.dropTable( "data_"+ t.name  );

                return true;
            }
                
            return false;
        }

        internal static Table getFields(string id)
        {
            
            try
            {
                int i = Convert.ToInt32(id);
                return Dal.TableDal.getTableById(i);
            }
            catch (Exception e)
            {
                return null;
            }
            
        }

        internal static DataTable getAllTable()
        {
            return Dal.TableDal.getAllTable();
        }

        internal static object addOrUpdateField(Field f)
        {
            if (f.id > 0)
            {
                Field old = TableDal.getField(f.id); //老字段
                //update
                if (Dal.TableDal.updateField(f.id, f.name, f.typeid, f.pid, f.sortid,f.search ) > 0)
               {
                    //TableDal.delColumn(delColunm,tb);
                    TableDal.updateColumn(old, f); //修改字段

                    ///return TableDal.updateColumn() > 0;
                    return true;
                }
            }
            else
            {
                //add
                if (Dal.TableDal.AddField( f.name, f.typeid, f.pid, f.sortid, f.search) != null)
                {
                    Table t = Dal.TableDal.getTableById(f.pid);//字段所属表格
                    return addColumn(f.name, t.name, f.dbtype);
                    
                }
            }
            
            return false;
        }

        public static bool addColumn(string column, string table, string dbtype)
        {
            bool b = Dal.TableDal.AddColumn(column, "data_"+table, dbtype)>0;
            return b;
        }
        /// <summary>
        /// 删除列，因为sqlite不支持，所以得自己实现
        /// </summary>
        /// <returns></returns>
        public static bool delColunm(string column, string table)
        {
            return Dal.TableDal.delColumn(column, table) > 0;
        }

        internal static Table getTableById(int id)
        {
            return Dal.TableDal.getTableById(id);
            //throw new NotImplementedException();
        }

        internal static void delField(string id)
        {
            try
            {
                int i = Convert.ToInt32(id);
                Dal.TableDal.delField(i);
            }
            catch (Exception e)
            {
            }
            
        }

        internal static Field getField(int id)
        {
            return Dal.TableDal.getField(id);
        }

        internal static bool addOrUpdateTable(Table t)
        {
            if (t.id == -1) //新增表
            {
                int id = Convert.ToInt32(Dal.TableDal.AddTable(t.name, t.ignore, t.sheetname));
                if (id != 0)
                {
                    //成功创建表
                    Dal.TableDal.createTable("data_" + t.name);
                    return true;
                }
                return false;
            }
            else
            {
                Table tb = TableDal.getTableById(t.id);
                //更新表
                int ret = Dal.TableDal.updateTable(t);
                if (ret>0) 
                {
                    if (!t.name.Equals(tb.name))
                    {
                        TableDal.modifyTable("data_" + tb.name, "data_" + t.name);
                    }
                    return true;
                }

                return false;
            }
        }
    }
}
